package org.ilovecode.display
{
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import gs.TweenLite;
	
	import org.ilovecode.animation.Animation;
	import org.ilovecode.animation.BaseObjectBlue;
	import org.ilovecode.animation.BaseObjectGreen;
	import org.ilovecode.events.ChooserEvent;
	import org.ilovecode.screen.Screen;
	import org.ilovecode.screen.ScreenName;
	
	public class Intro extends Screen
	{
		private var blueCircleAnimation:Animation;
		private var greenCircleAnimation:Animation;
		private var chooser:Chooser;
		
		public function Intro(type:String="", group:String="", name:String="")
		{
			super(ScreenName.INTRO, "", "intro");
		}
		
		override protected function construct():void
		{
			super.construct();
			
			var radius:int = ( (stage.stageWidth < stage.stageHeight) ? stage.stageWidth : stage.stageHeight ) * 1.5 - 200;
			var drawingArea:Rectangle = new Rectangle(stage.width / 2, stage.height - radius / 8, radius, radius);
			blueCircleAnimation = new Animation(new BaseObjectBlue(), "blue", 10, drawingArea);
			addChild(blueCircleAnimation);
			
			radius = 450;
			drawingArea = new Rectangle(stage.width / 2 + radius, 0, radius, radius);
			greenCircleAnimation = new Animation(new BaseObjectGreen(), "green", 10, drawingArea);
			addChild(greenCircleAnimation);
			
			chooser = new Chooser();
			addChild(chooser);
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			chooser.x = blueCircleAnimation.drawingArea.x - chooser.width / 2;
			chooser.y = blueCircleAnimation.drawingArea.y - blueCircleAnimation.drawingArea.height / 6;
			chooser.alpha = 0;
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			blueCircleAnimation.addEventListener(Event.COMPLETE, update);
			greenCircleAnimation.addEventListener(Event.COMPLETE, update);
			chooser.addEventListener(ChooserEvent.CHOOSER_CLICKED, update);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			blueCircleAnimation.removeEventListener(Event.COMPLETE, update);
			greenCircleAnimation.removeEventListener(Event.COMPLETE, update);
			chooser.removeEventListener(ChooserEvent.CHOOSER_CLICKED, update);
		}
		
		override protected function update(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					TweenLite.to(chooser, 0.5, { alpha:1 } );
					break;
				case ChooserEvent.CHOOSER_CLICKED:
					//TODO: Switch with controller functionality
					var value:Number = -stage.width / 4;
					TweenLite.to(blueCircleAnimation, 2, { x:value.toString() } );
					this.dispatchEvent(event);
					break;
				default:
					super.update(event);
					break;
			}
		}
		
		
	}
}